<template>
    <div>
        <Button type="info" @click="getType(1,$event)">新闻分类</Button>
        <Button type="success" @click="getType(2,$event)">上传分类</Button>
        <Button type="warning" @click="getType(3,$event)">产品分类</Button>
        <Tree :data="data5" :render="renderContent"></Tree>
        <Modal
            v-model="modal2"
            title="添加文件"
           >
            <Form ref="formValidate" :model="formItem" :label-width="80">
                <FormItem label="文件名">
                    <Input v-model="formItem.input" placeholder="请输入文件名"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="addCate()">提交</Button>
                    <Button type="ghost" style="margin-left: 8px">取消</Button>
                </FormItem>
            </Form>
        </Modal>
        <Modal
            v-model="modal3"
            title="修改文件名"
           >
            <Form ref="formValidate" :model="formItem" :label-width="80">
                <FormItem label="文件名">
                    <Input v-model="formItem.upInput" placeholder="请输入文件名"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="update()">保存</Button>
                    <Button type="ghost" style="margin-left: 8px">取消</Button>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                type:1,
                addVal:'',
                modal2: false,
                modal3: false,
                parentId:'',
                updateId:'',
                formItem: {
                    input: '',
                    upInput:'',
                },
                data5: [
                    {
                        title: '新闻分类',
                        expand: true,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h('Icon', {
                                        props: {
                                            type: 'ios-folder-outline'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '32px'
                                    }
                                }, [
                                    h('Button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'ios-plus-empty',
                                            type: 'primary'
                                        }),
                                        style: {
                                            width: '52px'
                                        },
                                        on: {
                                            click: () => { this.append(data) }
                                        }
                                    })
                                ])
                            ]);
                        },
                        children: [],
                    }
                ],
                buttonProps: {
                    type: 'ghost',
                    size: 'small',
                }
            }
        },
        methods: {
            renderContent (h, { root, node, data }) {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: 'ios-paper-outline'
                            },
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px'
                        }
                    }, [
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-plus-empty'
                            }),
                            style: {
                                marginRight: '8px'
                            },
                            on: {
                                click: () => { this.append(data) }
                            }
                        }),
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-minus-empty'
                            }),
                            style: {
                                marginRight: '8px'
                            },
                            on: {
                                click: () => { this.remove(root, node, data) }
                            }
                        }),
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-color-wand-outline'
                            }),
                            on: {
                                click: () => { this.updateAppend(data) }
                            }
                        })
                    ])
                ]);
            },
            append (data) {
                this.modal2=true;
                this.parentId=data._id;
            },
            addCate(){
                var newData={title:this.formItem.input,type:this.type,parentId:this.parentId}
                this.$http.post('http://localhost:3000/classify/data/',newData)
                .then(res=>{
                    this.modal2=false;
                    this.$Message.success('添加成功!');
                    this.getData();
                })
            },
            remove (root, node, data) {
                this.$Modal.confirm({
                    title: '警告！',
                    content: '<p>你确定要删除吗？</p>',
                    onOk: () => {
                        const parentKey = root.find(el => el === node).parent;
                        const parent = root.find(el => el.nodeKey === parentKey).node;
                        const index = parent.children.indexOf(data);
                        parent.children.splice(index, 1);
                        this.$http.delete('http://localhost:3000/classify/data/'+data._id)
                        .then(res=>{
                            console.log(res);
                        })
                    },
                    onCancel: () => {
                        this.$Message.info('点击了取消');
                    }
                });
            },
            update(){
                var newData={title:this.formItem.upInput}
                 this.$http.put('http://localhost:3000/classify/data/'+this.updateId,newData)
                .then(res=>{
                    this.modal3=false;
                    this.$Message.success('修改成功!');
                    this.getData();
                })
            },
            updateAppend(data){
                this.modal3=true;
                this.updateId=data._id;
                console.log(this.$data.formValidate);
            },
            getData(){
                this.$http.get('http://localhost:3000/classify/list/'+this.type)
                .then(res=>{
                    this.data5[0].children=res.data;
                })
            },
            getType(res,e){ 
                this.data5[0].title=e.target.innerText;
                this.type=res;
                this.getData();
            }
        },
        created(){
             this.getData();
        }
    }
</script>